package com.zdp.rocketmq.send;

import com.zdp.rocketmq.config.JmsConfig;
import com.zdp.rocketmq.producer.ProducerBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author sesshomaru
 * @date 2021/6/21
 * 发送速度很快但是没有保障的发送方式
 * 无需等待
 * 主要可以用来做日志采集，适用于某些耗时非常短，但是对可靠性要求不高的场景
 */
@Slf4j
@RestController
public class OneWayController {

    @Autowired
    public ProducerBuilder producerBuilder;

    @RequestMapping("/api/v1/oneway")
    public Object send(String keys, String tag) throws RemotingException, MQClientException, InterruptedException {
        Message message = new Message(JmsConfig.GENERAL_TOPIC,tag,keys,("hello rocketmq = " + keys).getBytes());
        producerBuilder.getProducer().sendOneway(message);
        log.info("发送OneWay消息成功:{}", message.toString());
        return new HashMap<>(1);
    }
}
